package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.Productlist;
import com.ruoyi.system.domain.dto.ProductDto;
import com.ruoyi.system.domain.dto.StorelistDto;
import com.ruoyi.system.domain.vo.ProductlistVo;
import com.ruoyi.system.mapper.ProductlistMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.StorelistMapper;
import com.ruoyi.system.domain.Storelist;
import com.ruoyi.system.service.IStorelistService;

/**
 * 门店信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-30
 */
@Service
public class StorelistServiceImpl implements IStorelistService {
    @Autowired
    private StorelistMapper storelistMapper;

    @Autowired
    private ProductlistMapper productlistMapper;

    /**
     * 查询门店信息
     *
     * @param storeId 门店信息主键
     * @return 门店信息
     */
    @Override
    public StorelistDto selectStorelistByStoreId(Long storeId) {
        StorelistDto storelistDto = storelistMapper.selectStorelistByStoreId(storeId);
        List<ProductDto> productList = productlistMapper.selectByStoreId(String.valueOf(storeId));
        for (ProductDto item : productList) {
            String productImage = item.getProductImage();
            String[] splitImg = productImage.split(",");
            item.setProductImage(splitImg[0]);
        }
        storelistDto.setProducList(productList);

        return storelistDto;
    }

    /**
     * 查询门店信息列表
     *
     * @param storelist 门店信息
     * @return 门店信息
     */
    @Override
    public List<StorelistDto> selectStorelistList(Storelist storelist) {
        List<StorelistDto> storelistList = storelistMapper.selectStorelistList(storelist);
        List<ProductDto> productList = productlistMapper.selectProductAllStoreList();

        // 使用 Map 来存储每个门店对应的产品列表
        Map<String, List<ProductDto>> productMap = new HashMap<>();
        for (ProductDto productDto : productList) {
            String productImage = productDto.getProductImage();
            String[] splitImg = productImage.split(",");
            productDto.setProductImage(splitImg[0]);
            String storeId = productDto.getStoreId();
            productMap.computeIfAbsent(storeId, k -> new ArrayList<>()).add(productDto);
        }


        // 遍历门店列表，为每个门店设置对应的产品列表
        for (StorelistDto store : storelistList) {
            String storeId = String.valueOf(store.getStoreId());
            List<ProductDto> productDtoList = productMap.getOrDefault(storeId, new ArrayList<>());
            store.setProducList(productDtoList);
        }

        return storelistList;
    }

    /**
     * 查询门店信息列表
     *
     * @return 门店信息
     */
    @Override
    public List<Storelist> selectStoreBaseList() {
        return storelistMapper.storeBaseList();
    }

    /**
     * 新增门店信息
     *
     * @param storelist 门店信息
     * @return 结果
     */
    @Override
    public int insertStorelist(Storelist storelist) {
        storelist.setCreateTime(DateUtils.getNowDate());
        return storelistMapper.insertStorelist(storelist);
    }

    /**
     * 修改门店信息
     *
     * @param storelist 门店信息
     * @return 结果
     */
    @Override
    public int updateStorelist(Storelist storelist) {
        return storelistMapper.updateStorelist(storelist);
    }

    /**
     * 批量删除门店信息
     *
     * @param storeIds 需要删除的门店信息主键
     * @return 结果
     */
    @Override
    public int deleteStorelistByStoreIds(Long[] storeIds) {
        return storelistMapper.deleteStorelistByStoreIds(storeIds);
    }

    /**
     * 删除门店信息信息
     *
     * @param storeId 门店信息主键
     * @return 结果
     */
    @Override
    public int deleteStorelistByStoreId(Long storeId) {
        return storelistMapper.deleteStorelistByStoreId(storeId);
    }

    @Override
    public int addStoreProduct(Long storeId, Long productId) {

        return storelistMapper.selectStoreProductCount(storeId, productId) > 0 ? 0 : storelistMapper.addStoreProduct(storeId, productId);
    }
}
